import pandas as pd

url = "https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/Data/ml-basics/penguins.csv"
df = pd.read_csv(url)
print(df.head())
import matplotlib.pyplot as plt
import seaborn as sns

sns.set()  # 设置seaborn的风格
plt.figure(figsize=(10, 6))
sns.countplot(x='Species', data=df)
plt.title('Distribution of Penguins Species')
plt.show()

plt.figure(figsize=(10, 6))
sns.boxplot(data=df, x='Species', y='FlipperLength')
plt.title('Flipper Length by Species')
plt.show()

plt.figure(figsize=(10, 6))
sns.boxplot(data=df, x='Species', y='CulmenLength')
plt.title('Culmen Length by Species')
plt.show()

plt.figure(figsize=(10, 6))
sns.boxplot(data=df, x='Species', y='CulmenDepth')
plt.title('Culmen Depth by Species')
plt.show()
print(df.isnull().sum())  # 查看每列的缺失值数量
df.dropna(inplace=True)  # 删除包含缺失值的行
from sklearn.model_selection import train_test_split

# 特征和标签
X = df[['CulmenLength', 'CulmenDepth', 'FlipperLength']]
y = df['Species']

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
from sklearn.linear_model import LogisticRegression

# 创建多类别逻辑回归模型
model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=1000)

# 训练模型
model.fit(X_train, y_train)
# 预测测试集的标签
y_pred = model.predict(X_test)

# 计算模型的准确率
accuracy = model.score(X_test, y_test)
print(f'Model Accuracy: {accuracy}')